package com.embedsky.administrator.mycardreader.p2p;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import java.util.Collections;

/**
 * @Des:
 * @Author: hang.zhao
 * @Date: 2023/2/23
 */
public class WifiP2pBroadcastReceive extends BroadcastReceiver {

    private String TAG = WifiP2pBroadcastReceive.class.getSimpleName();

    private WifiP2pManager wifiP2pManager;
    private WifiP2pManager.Channel channel;
    private DirectActionListen listen;

    public WifiP2pBroadcastReceive(WifiP2pManager wifiP2pManager, WifiP2pManager.Channel channel, DirectActionListen listen) {
        this.wifiP2pManager = wifiP2pManager;
        this.channel = channel;
        this.listen = listen;
    }


    public static IntentFilter getIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
        return intentFilter;
    }


    @RequiresApi(api = Build.VERSION_CODES.Q)
    @SuppressLint("MissingPermission")
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION:
                Log.d(TAG, "onReceive: "+intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1));
                boolean enable = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1) == WifiP2pManager.WIFI_P2P_STATE_ENABLED;
                listen.wifiP2pEnabled(enable);
                if (!enable) {
                    listen.onPeersAvailable(Collections.emptyList());
                }
                Log.d(TAG, "onReceive: WIFI_P2P_STATE_CHANGED_ACTION==" + enable);
                break;
            case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION:
                wifiP2pManager.requestPeers(channel, peers -> listen.onPeersAvailable(peers.getDeviceList()));
                break;
            case WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION:
                NetworkInfo info = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);
                if (info != null && info.isConnected()) {
                    wifiP2pManager.requestConnectionInfo(channel, wifiP2pInfo -> listen.onConnectionInfoAvailable(wifiP2pInfo));
                    Log.d(TAG, "onReceive: p2p连接成功");
                } else {
                    listen.onDisconnection();
                    Log.d(TAG, "onReceive: p2p连接失败");
                }
                break;
            case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION:
                WifiP2pDevice device = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
                if (device != null) {
                    listen.onGetDeviceInfo(device);
                    wifiP2pManager.requestDeviceInfo(channel, deviceInfo -> Log.d(TAG, "onReceive: " + deviceInfo));
                }
                break;
        }
    }
}
